package com.thoughtworks.yuyijq.generate.template;

import java.io.BufferedInputStream;

/**
 * User: yuyijq
 * Date: Oct 10, 2010
 * Time: 10:18:37 AM
 */
public class TemplateLoader {
    public static String load(String resourceLocation) {
        try {
            StringBuffer buffer = new StringBuffer();
            BufferedInputStream stream = new BufferedInputStream(TemplateLoader.class.getResourceAsStream(resourceLocation));

            for (int i = stream.read(); i != -1; i = stream.read()) {
                buffer.append((char) i);
            }

            return fixLineBreaks(buffer.toString());
        } catch (Exception e) {
            return null;
        }
    }

     private static String fixLineBreaks(String s) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (Character.LINE_SEPARATOR != c) {
                sb.append(c);
            }
        }

        return sb.toString();
    }
}
